package leetCode.hw.math;

public class HJ107Solution implements HJ107{

    /**
     * 缩小边界法
     * @param num
     * @return
     */
    @Override
    public double dichotomy(double num) {
        double right, left, mid;
        right = Math.max(num, 1.0);
        left = Math.min(num, -1.0);
        while (right - left > 0.001) {
            mid = (left + right) / 2;
            if (mid * mid * mid > num) {
                right = mid;
            } else if (mid * mid * mid < num) {
                left = mid;
            }
            // 找到结果,直接返回
            else
                return mid;
        }
        return right;
    }

}
